<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_news extends CI_Controller {
    private $limit = 20;
    function __construct() {
        parent::__construct();
        $this->load->model('model_news', 'm_news', TRUE);
    }
    
    function index($offset = 0, $order_column = 'datecreated', $order_type = 'DESC') {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            
            $data = array(
                'web_title' =>  'Admin News',
                'main_view' =>  'mimin/news/index_new',
                'list_data' =>  $this->m_news->get_all_news(),
            );
            $this->load->view('mimin/template/template_table',$data);
        }
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            $this->form_validation->set_rules('judul','Judul','required');
            $this->form_validation->set_rules('content','Isi Konten','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'             =>  'Data Berita Baru',
                    'msg_error_username'    =>  form_error('judul','<caption>','</caption>'),
                    'msg_error_name'        =>  form_error('content','<caption>','</caption>'),
                    'main_view'             =>  "mimin/news/add"
                );
                $this->load->view("mimin/template/template_editor",$data);
            }
            else{
                $config['upload_path'] = './content/media/news/';
                $config['allowed_types'] = 'jpg|jpeg|png';
                $this->upload->initialize($config);  
                $this->load->library('upload', $config);
                if(!$this->upload->do_upload("userfile")){
                    $data = array(
                        "main_view"     =>  "mimin/news/add",
                        "web_title"     =>  "Upload New News Data",
                        "error"         =>  $this->upload->display_errors("","")
                    );
//                    exit($this->upload->display_errors());
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d H:i:s");
                    $upload_image = $this->upload->data();
                    $isi = array(
                        'judul'         =>  $this->input->post('judul'),
                        'content'       =>  $this->input->post('content'),
                        'datecreated'   =>  $curdate,
                        'datemodified'  =>  $curdate,
                        'state_id'      =>  $this->session->userdata('admin_user')->state_id,
                        'image'         =>  'content/media/news/'.$upload_image['file_name']
                    );
                    if($this->m_news->save($isi)){
                        $data = array(
                            "main_view"     =>  "mimin/news/add",
                            "web_title"     =>  "Upload New News Data",
                            "success_status"    =>  1
                        );
                        $this->load->view("mimin/template/template_editor",$data);
                    }
                }
            }
        }
    }
    function delete($id=""){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_news","refresh");
        }
        else{
            if($this->m_news->delete($id)){
                redirect("admin_news","refresh");
            }
        }
    }
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($id==""){
            redirect("admin_news","refresh");
        }
        else{
            $count = $this->m_news->select_by_id($id,"count");
            if($count > 0){
                $this->form_validation->set_rules('judul','Judul','required');
                $this->form_validation->set_rules('content','Isi Konten','required');
                if($this->form_validation->run() == FALSE){
                    $data = array(
                        "main_view"     =>  "mimin/news/detail",
                        "web_title"     =>  "Detail News",
                        "list_data"     =>  $this->m_news->select_by_id($id)
                    );
                    $this->load->view("mimin/template/template_editor",$data);
                }
                else{
                    $config['upload_path'] = './content/media/news/';
                    $config['allowed_types'] = 'jpg|jpeg|png';
                    $this->load->library('upload', $config);
                    //IF THE ADMIN Do Not Choose any image to upload
                    if (!$this->upload->do_upload()){
                        $isi_update = array(
                                "judul"         =>  $this->input->post("judul"),
                                "content"       =>  $this->input->post("content"),
                                "datecreated"   =>  $this->input->post("tanggal_news"),
                                "datemodified"  =>  $curdate,
                                "modifiedby"    =>  $this->session->userdata('admin_user')->id_admin,
                            );
                        $status_update = $this->m_news->update($id,$isi_update);
                        if($status_update == false){
                            echo"false";
                        }
                        else{
                            $data = array(
                                "main_view"         =>  "mimin/news/detail",
                                "web_title"         =>  "Successfull Update News Data",
                                "success_status"    =>  1,
                                "list_data"     =>  $this->m_news->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                    }
                    else{
                        $upload_image = $this->upload->data();
                        $isi_update = array(
                            'judul'         =>  $this->input->post('judul'),
                            'content'       =>  $this->input->post('content'),
                            "datecreated"   =>  $this->input->post("tanggal_news"),
                            'datemodified'  =>  $curdate,
                            'image'         =>  'content/media/news/'.$upload_image['file_name']
                        );
                        $status_update = $this->m_news->update($id,$isi_update);
                        if($status_update){
                            $data = array(
                                "main_view"         =>  "mimin/news/detail",
                                "web_title"         =>  "Successfull Update News Data",
                                "success_status"    =>  1,
                                "list_data"         =>  $this->m_news->select_by_id($id)
                            );
                            $this->load->view("mimin/template/template_editor",$data);
                        }
                        else{
                            echo"Cannot update data";
                        }
                    }
                }
            }
            else{
                redirect("admin_news","refresh");
            }
        }
    }
}

/* End of file admin_news.php */
/* Location: ./application/controllers/admin_event.php */